package com.i8office.android.zhihuixinwu.ui.schedule;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.cyan.tools.annotation.ActivityFragmentInject;
import com.cyan.tools.base.BaseActivity;
import com.cyan.tools.base.BaseFragment;
import com.gyf.barlibrary.ImmersionBar;
import com.i8office.android.zhihuixinwu.R;
import com.i8office.android.zhihuixinwu.module.contract.CalendarContract;
import com.i8office.android.zhihuixinwu.ui.LeaderScheduleHomeFragment;
import com.i8office.android.zhihuixinwu.util.ViewUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


@ActivityFragmentInject(
        contentViewId = R.layout.activity_schedule
)
public class ScheduleActivity extends BaseActivity<CalendarContract.SchedulePresenter> implements CalendarContract.ScheduleView {
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    List<String> titles = new ArrayList<>();
    private List<BaseFragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f) //原理：如果当前设备支持状态栏字体变色，会设置状态栏字体为黑色，如果当前设备不支持状态栏字体变色，会使当前状态栏加上透明度，否则不执行透明度
                .init();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mPresenter = new SchedulePresenter(this);
        mPresenter.checkInGroup();
    }

    private void initViewPager(CheckInGroup checkInGroup) {
        //    List<String> titles = {"领导日程", "领导日程设置", "个人日程", "列表"};


//        String url = Constants.BASE_URL + "/Apps/Cal/Index/MobileLeaderCal?&__session__=" + AppApplication.getSession();
        if (checkInGroup.LeaderView) {
            titles.add("领导日程");
//            fragments.add(WebFragment.newInstance(url));
            fragments.add(LeaderScheduleHomeFragment.newInstance());
        }
        if (checkInGroup.LeaderSet) {
            titles.add("领导日程设置");
            fragments.add(LeaderCalendarSettingFragment.newInstance());
        }
        titles.add("个人日程");
        fragments.add(SelfCalendarSettingFragment.newInstance(checkInGroup.LeaderSet));
        titles.add("列表");
        fragments.add(ScheduleListFragment.newInstance(checkInGroup.LeaderSet));

        tabLayout.setVisibility(View.VISIBLE);
        viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return titles.get(position);
            }

            @Override
            public int getCount() {
                return titles.size();
            }
        });
        tabLayout.setupWithViewPager(viewpager);
        ViewUtil.dynamicSetTabLayoutMode(tabLayout, 45, 10);
        viewpager.setCurrentItem(getIntent().getIntExtra("page", 0));

    }

    @Override
    public void showCheckInGroup(CheckInGroup checkInGroup) {
        initViewPager(checkInGroup);
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }
}
